package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketLog;
import com.powernobug.bean.model.MarketLogExample;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.LogListParam;
import com.powernobug.mapper.MarketLogMapper;
import com.powernobug.service.AdminLogService;
import com.powernobug.util.MyBatisUtil;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-27
 * @Description:
 */
public class AdminLogServiceImpl implements AdminLogService {
    private static SqlSession tempSession;

    @Override
    public PageVo getLogByPage(LogListParam params) {
        MarketLogExample example = new MarketLogExample();
        if (params.getPage() == null || params.getLimit() == null) {
            throw new PersistenceException();
        }
        if (!(params.getSort() == null) && !(params.getOrder() == null)) {
            example.setOrderByClause(params.getSort() + " " + params.getOrder());
        }

        MarketLogExample.Criteria criteria = example.createCriteria();
        if (params.getName() != null) {
            criteria.andAdminLike("%" + params.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);

        tempSession = MyBatisUtil.getSqlSession();
        MarketLogMapper mapper = tempSession.getMapper(MarketLogMapper.class);

        PageHelper.startPage(params.getPage(), params.getLimit());
        List<MarketLog> marketLogs = mapper.selectByExample(example);
        return PageVo.pag(marketLogs);
    }
}
